import settings
from book import router as book_router
from events import startup_handler
from fastapi import FastAPI
from fastapi.middleware.cors import CORSMiddleware

origins = [
    "http://127.0.0.1:3000",
    "http://localhost:3000",
    "http://127.0.0.1",  # 本地部署用
    "http://localhost",
]


def create_app() -> FastAPI:
    app = FastAPI()

    app.add_middleware(
        CORSMiddleware,
        allow_origins=origins,
        allow_credentials=True,
        allow_methods=["*"],
        allow_headers=["*"],
    )
    app.add_event_handler("startup", startup_handler(app))
    # app.add_event_handler("shutdown", shutdown_handler(app))
    app.include_router(book_router, prefix='/api', tags=['book应用'])
    return app


app = create_app()

if __name__ == '__main__':
    import uvicorn

    uvicorn.run('main:app', debug=settings.DEBUG, reload=settings.DEBUG)
